from tkinter import *
from DmjTool import Getdm
import threading

class Dmwin:
    def ready(self,roomid,textsize,sizex,sizey,textcl):
        self.textcl = textcl
        self.roomid = roomid
        self.textsize = textsize
        self.sizex = sizex
        self.sizey = sizey
        self.getdm = Getdm(int(roomid),10,self.rundef)
        self.isend = True
        self.root=Tk()
        self.root.geometry(str(sizex)+'x'+str(sizey))
        #self.root.attributes("-alpha", 0.5)
        self.root.wm_attributes('-topmost',1)
        self.root.attributes("-transparentcolor","dimgray")
        self.root["background"] = "dimgray"
        self.root.title('弹幕框')
        self.text0 = Text (self.root,bg = 'dimgray',fg = self.textcl,font=("黑体", int(self.textsize)))

    def rundef(self,dm):#用于弹幕更新
        text = ''
        text = dm['nickname']+'：'+dm['text']+'\n\n'
        self.text0.config(state=NORMAL)
        self.text0.insert(END, text)#向Text框体中写入弹幕
        self.text0.see('end')#跳到text框体的最后面
        self.text0.config(state=DISABLED)

    def dmall(self):#进入获取弹幕的循环
        while self.isend:
            self.getdm.newalldm()

    def winrun(self):
        self.text0.pack(fill = BOTH,side = LEFT)
        self.t = threading.Thread(target=self.dmall)#把获取弹幕的循环放入子线程
        self.t.start()
        self.text0.config(state=DISABLED)
        self.root.mainloop()

    def winend(self):
        self.isend = False
        self.root.destroy()

    def winlock(self):
        self.root.overrideredirect(True)
        self.root.wm_attributes('-disabled',1)
